<script lang="ts" setup>
import { useRouter } from 'vue-router'

const props = defineProps<{
  title?: string
  rightText?: string
  back?: () => void
  // back?(): void
}>()

const router = useRouter() // this.$router
// const route = useRoute() // this.$route

const onClickLeft = () => {
  if (props.back) return props.back()
  // 根据有没有历史记录决定是 back 还是直接返回到首页
  if (history.state.back) {
    router.back()
  } else {
    router.push('/')
  }
}

const emits = defineEmits<{
  (e: 'clickRight', event: MouseEvent): void
}>()

const onClickRight = (e: MouseEvent) => {
  emits('clickRight', e)
}
</script>
<template>
  <van-nav-bar
    :title="title"
    left-text="返回"
    left-arrow
    :right-text="rightText"
    fixed
    @click-left="onClickLeft"
    @click-right="onClickRight"
  />
</template>

<style scoped lang="scss">
::v-deep() {
  .van-nav-bar {
    &__arrow {
      font-size: 18px;
      color: var(--cp-text1);
    }

    &__text {
      font-size: 15px;
    }
  }
}
</style>
